package com.ludomancy.archeologyrpg;

import java.awt.Image;
import java.awt.Point;
import java.util.Iterator;
import java.util.Vector;
import com.ludomancy.archeologyrpg.entities.Rocket;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Sound;
import com.ludomancy.gamelib.Tileset;

public class Game
{
	public static final int TILE_SIZE = 20;
	public static final int ENTITY_TILE_SIZE = 32;
	public static int LANDSCAPE_WIDTH = 700;
	public static int LANDSCAPE_HEIGHT = 438;
	public static final int TIME_PER_UPDATE = 20;
	
	public static Game game;
	public Vector entities = new Vector();
	public Tileset landscape;
	public Entity activeEntity = null;
	public Point exploringPoint = null;
	public Rocket rocket;
	
	public Vector photographies = new Vector();
	public static final int PHOTO_SIZE = 58;
	public static final int PHOTO_MAX = 6;
	
	// player info
	public int maxExplorationRadius = 30;
	
	public Game()
	{
		Image background = ResourceManager.getImage("background.png");
		LANDSCAPE_WIDTH = background.getWidth(null);
		LANDSCAPE_HEIGHT = background.getHeight(null);
		game = this;
		
		LevelGenerator.generate(this);
	}
	
	public static int dist(Point from, Entity entity)
	{
		int a = (from.x - entity.px) * (from.x - entity.px);
		int b = (from.y - entity.py) * (from.y - entity.py);
		return (int) Math.sqrt(a+b);
	}
	
	public Entity getClosestEntity(Point from, Class type)
	{
		Entity closest = null;
		int bestDistance = 100000;
		for (Iterator iter = entities.iterator(); iter.hasNext();)
		{
			Entity entity = (Entity) iter.next();
			if(type.isInstance(entity))
			{
				int distance = dist(from, entity);
				if(distance < bestDistance)
				{
					closest = entity;
				}
			}
		}		
		return closest;
	}
	
	public Vector getEntitiesInArea(Point pos, int radius)
	{
		Vector ret = new Vector();
		for (Iterator iter = entities.iterator(); iter.hasNext();)
		{
			Entity entity = (Entity) iter.next();
			int distance = dist(pos, entity);
			if(distance <= radius)
			{
				ret.add(entity);
			}
		}
		return ret;
	}
	
	public void update()
	{
		boolean timeRunning = false;
		
		if(exploringPoint != null)
		{
			timeRunning = true;
			Vector inArea = getEntitiesInArea(exploringPoint, maxExplorationRadius);
			for (int i = 0; i < inArea.size(); i++)
			{
				Entity entity = (Entity) inArea.elementAt(i);
				if(!entity.isVisible())
				{
					entity.explore();
				}
			}
		}
		
		timeRunning = timeRunning || activeEntity != null;
		
		for (int i = 0; i < entities.size(); i++)
		{
			Entity entity = (Entity) entities.elementAt(i);
			entity.update(timeRunning, activeEntity == entity && activeEntity.isActive());
		}
		// remove dead entities
		for (Iterator iter = entities.iterator(); iter.hasNext();)
		{
			Entity entity = (Entity) iter.next();
			if(entity.dead)
			{
				iter.remove();
			}
		}
	}

	public static int discretize(int time)
	{
		return time / TIME_PER_UPDATE;
	}

	public void addPicture(Entity recordedEntity)
	{
		if(recordedEntity != null)
		{
			photographies.add(recordedEntity);
			Sound s = new Sound("photo.wav");
			s.play();
		}
	}


	public Entity getAnyUnrevealedEntity()
	{
		for (Iterator iter = entities.iterator(); iter.hasNext();)
		{
			Entity entity = (Entity) iter.next();
			if(!entity.isVisible() && !this.photographies.contains(entity))
			{
				return entity;
			}
		}
		return null;
	}


	public void showHint(String hint)
	{
		ArcheologyRPGApplet.app.gameCanvas.showWidgetModal(new HintWidget(ArcheologyRPGApplet.mainWidget, hint, "OK" ));
	}
	
}
